package ntut.andengine.game;

import ntut.andengine.physics.ContactEvent;
import ntut.andengine.physics.PhysicalComponent;
import ntut.andengine.physics.DeathLight.Direction;
import ntut.andengine.physics.PhysicalComponent.ComponentColor;
import ntut.andengine.physics.PhysicalFactory;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class GameLevel19 extends GameLevel {

GameLevel mLevel;
	
	@Override
	public void onLoadResources() {
		super.onLoadResources();
		mLevel = this;
		
		BitmapTexture backgroundBitmapTexture = new BitmapTexture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion backgroundTextureRegion = BitmapTextureRegionFactory.createFromAsset(backgroundBitmapTexture, this, "universe.png", 0, 0);
		mEngine.getTextureManager().loadTexture(backgroundBitmapTexture);
		
		Sprite background = new Sprite(0, 0, backgroundTextureRegion);
		mScene.attachChild(background);
	}
	
	@Override
	public Scene onLoadScene() {

        PhysicalComponent gate = PhysicalFactory.CreateTransmissionGate(this, new Vector2(100, 230));
        gate.getBody().setUserData(new ContactEvent("Gate"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
				
				// LEVEL COMPLETE!!
				if(body.getUserData().toString() == "Player"){
					mLevel.runOnUiThread(new Runnable(){

						@Override
						public void run() {
							mMusic.pause();
							successSound.play();
							Toast.makeText(mLevel, "Level Completed!", Toast.LENGTH_SHORT).show();	
						}
			        	
			        });
					
					startActivity(GameLevel19.this, GameLevel7.class);
				}
			
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {
				// TODO Auto-generated method stub
				
			}
		
		});
        
        PhysicalFactory.CreateWall(this, new Vector2(100, 140), 200, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(20, 440), 40, 560, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(260, 700), 440, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(460, 560), 40, 240, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(180, 320), 40, 320, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(440, 420), 80, 40, ComponentColor.White);
        PhysicalFactory.CreateBox(this, new Vector2(400, 360), ComponentColor.Blue);
        PhysicalFactory.CreateBox(this, new Vector2(360, 360), ComponentColor.Red);
        PhysicalFactory.CreateBox(this, new Vector2(360, 280), ComponentColor.Red);
        PhysicalFactory.CreateBox(this, new Vector2(320, 280), ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(260, 240), 40, 80, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(320, 220), 80, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(400, 220), 80, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(260, 340), 120, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(300, 380), 40, 40, ComponentColor.White);
        PhysicalFactory.CreateBox(this, new Vector2(200, 520), ComponentColor.Red);
        PhysicalFactory.CreateBox(this, new Vector2(200, 520), ComponentColor.Blue);
        PhysicalFactory.CreateLaunchingPad(this, new Vector2(80, 600), true);
        PhysicalFactory.CreateDeathLight(this, new Vector2(180, 600), 120, 40, Direction.Right);
        
        mSparkle = PhysicalFactory.CreateSparkle(this, new Vector2(80, 40), ComponentColor.Blue);
	
		this.mScene.registerUpdateHandler(this.mPhysicsWorld);	

		return mScene;
	}
}
